﻿using Butcher.winform.EnterpriseForms.DivisionStorage;
using Butcher.winform.EnterpriseForms.Fatoiles;
using Butcher.winform.EnterpriseForms.MarketWeigh;
using Butcher.winform.EnterpriseForms.MessSalguhterPut;
using Butcher.winform.EnterpriseForms.Partitionees;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;

namespace Butcher.winform.EnterpriseForms
{
    public partial class ScaleBackfill : Form
    {
        public static int pid = 0;
        public static string TxtVal = "";
        public ScaleBackfill()
        {
            InitializeComponent();
        }
        public class Scalepagesize
        {

            public int pageinedex { get; set; } = 1;

            public int pagesize { get; set; } = 50;
            public string? ScaleName { get; set; } = null;
        };
        private void uiFlowLayoutPanel1_Load(object sender, EventArgs e)
        {
            int counts = 0;
            Scale str = new Scale();

            Scalepagesize condition = new Scalepagesize();
            string url = @"http://localhost:5229/api/Section/ScaleDisplayHtml";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Scalepagesize>(condition), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["lists"].Value<JArray>();
            var count = josnresult["totalCount"];
            counts = Convert.ToInt32(count);
            List<Scale> ssss = results.ToObject<List<Scale>>();


            Button[] buttons = new Button[counts];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new Button();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                buttons[i].Location = new Point(i * 130 + 130, 30);
                Scale asas = ssss[i];
                //转换为字符串
                buttons[i].Name = Json.Serialize(asas); 
                //转换为实体
                Scale babab = Json.Deserialize<Scale>(buttons[i].Name);
                buttons[i].Size = new Size(100, 80);
                buttons[i].BackColor = Color.White;
                buttons[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                buttons[i].Text = ssss[i].ScaleName;
                buttons[i].Click += new System.EventHandler(this.button_Click);
                uiFlowLayoutPanel1.Controls.Add(buttons[i]);
            }
        }
        private void button_Click(object sender, EventArgs e)
        {
            //int counts = 0;
            //Scalepagesize condition = new Scalepagesize();
            //string url = @"http://localhost:5229/api/Section/ScaleDisplayHtml";
            //HttpClient client = new HttpClient();
            //var content = new StringContent(JsonSerializer.Serialize<Scalepagesize>(condition), Encoding.UTF8);
            //content.Headers.Remove("Content-Type");
            //content.Headers.Add("Content-Type", "application/json");
            //string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            //JObject josnresult = JObject.Parse(result);
            //var results = josnresult["lists"].Value<JArray>();
            //var count = josnresult["totalCount"];
            //counts = Convert.ToInt32(count);
            //List<Scale> ssss = results.ToObject<List<Scale>>()


            Button button = (Button)sender;
            TxtVal = button.Name;
            ScaleName.Text = button.Text;
            MessageBox.Show(button.Name);
            if (pid == 1)
            {
                LiveAnimalweighings departmentview = new LiveAnimalweighings();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 2)
            {
                WhiteNoteList departmentview = new WhiteNoteList();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 3)
            {
                PartitionList departmentview = new PartitionList();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 4)
            {
                WeighingScaleShow departmentview = new WeighingScaleShow();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 5)
            {
                FatoilList departmentview = new FatoilList();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 6)
            {
                DivisionStorageAdd departmentview = new DivisionStorageAdd();
                this.Hide();
                departmentview.ShowDialog();
            }
            if (pid == 7)
            {
                MarketWeighAdd departmentview = new MarketWeighAdd();
                this.Hide();
                departmentview.ShowDialog();
            }
        }

        private void uiPanel3_Load(object sender, EventArgs e)
        {

        }     
        

    }
}
